/*******************************************************************************
 * @project: CDMS
 * @package: com.esoon.cdms.action
 * @file: ProjectAction.java
 * @author: zhangpei
 * @created: 2017-2-13
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

/**
 * 
 */
package com.esoon.cdms.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import com.esoon.cdms.util.CapitalWebServiceUtil;
import com.esoon.cdms.util.JaxbUtil;
import com.mentor.harness.schema.bridgesdesigns.DesignType;
import com.mentor.harness.schema.bridgesdesigns.DesignsType;
import com.mentor.harness.schema.bridgesprojects.ProjectType;
import com.mentor.harness.schema.bridgesprojects.ProjectsType;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Administrator
 * 
 */
public class ProjectAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4725986563977107531L;

	public static void main(String[] args) throws Exception {
		ProjectAction pa = new ProjectAction();
		pa.getAllProject();
	}

	private ProjectsType projectsType;
	private DesignsType designsType;
	private ProjectType project;

	/**
	 * @return the project
	 */
	public ProjectType getProject() {
		return project;
	}

	/**
	 * @param project
	 *            the project to set
	 */
	public void setProject(ProjectType project) {
		this.project = project;
	}

	/**
	 * @return the designsType
	 */
	public DesignsType getDesignsType() {
		return designsType;
	}

	/**
	 * @param designsType
	 *            the designsType to set
	 */
	public void setDesignsType(DesignsType designsType) {
		this.designsType = designsType;
	}

	/**
	 * @return the projectsType
	 */
	public ProjectsType getProjectsType() {
		return projectsType;
	}

	/**
	 * @param projectsType
	 *            the projectsType to set
	 */
	public void setProjectsType(ProjectsType projectsType) {
		this.projectsType = projectsType;
	}

	/**
	 * 根据chs接口获取所有项目信息的xml文件，然后解析xml文件得到项目对象集合
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllProject() throws Exception {
		File xmlFile = new File("E:/workspace_capital/CDMS/temp/Projects.xml");
		// File xmlFile = CapitalWebServiceUtil.getAllProjectXml();
		try {
			InputStream is = new FileInputStream(xmlFile);
			byte[] bytes = new byte[100];
			String xmlStr = "";
			int temp;
			while ((temp = is.read(bytes)) != -1) {
				xmlStr += new String(bytes, 0, temp);
			}
			// System.out.println(xmlStr);

			projectsType = (ProjectsType) JaxbUtil.xml2Java(ProjectsType.class,
					xmlStr);

			// 把项目集合保存到session对象中
			Map session = ActionContext.getContext().getSession();
			for (ProjectType project1 : projectsType.getProject()) {
				System.out.println(project1.toString());
			}
			session.remove("projects");
			session.clear();
			session.put("projects", projectsType.getProject());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "projects";
	}

	/**
	 * 这个方法后面完善的时候需要传递项目名称String projectName
	 * 
	 * @return
	 * @throws Exception
	 */
//	public String getAllDesignByProjectName() throws Exception {
//		// File xmlFile = new File(
//		// "E:/workspace_capital/CDMS/temp/Designslist_12Test.xml");
//		File xmlFile = CapitalWebServiceUtil.getAllDesignByProjectName(project
//				.getName());
//		// File xmlFile = CapitalWebServiceUtil.getAllProjectXml();
//		try {
//			InputStream is = new FileInputStream(xmlFile);
//			byte[] bytes = new byte[100];
//			String xmlStr = "";
//			int temp;
//			while ((temp = is.read(bytes)) != -1) {
//				xmlStr += new String(bytes, 0, temp);
//			}
//			// System.out.println(xmlStr);
//
//			designsType = (DesignsType) JaxbUtil.xml2Java(DesignsType.class,
//					xmlStr);
//
//			// 把项目集合保存到session对象中
//			Map session = ActionContext.getContext().getSession();
//			for (DesignType design : designsType.getDesign()) {
//				System.out.println(design.toString());
//			}
//			session.remove("designs");
//			session.clear();
//			session.put("designs", designsType.getDesign());
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return "designs";
//	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
